package cos.entity;

public class Song {
	private final String artist;
	private final String title;

	public Song(String artist, String title) {
		// ololo
		if (artist.equals("Tiger Lillies"))
			artist = "The Tiger Lillies";
		else if (artist.equals("Lena"))
			artist = "Lena Meyer-Landrut";

		this.artist = artist;
		this.title = title;
	}

	public String getArtist() {
		return artist;
	}

	public String getTitle() {
		return title;
	}

	/**
	 * get artist name with special symbols
	 */
	public String getArtistWithoutSpaces() {
		return replaceSpecialSymbols(artist);
	}

	/**
	 * get song title with special symbols
	 */
	public String getTitleWithoutSpaces() {
		return replaceSpecialSymbols(title);
	}

	/**
	 * get artist name with special symbols
	 */
	public String getArtistForFile() {
		return replaceFileSymbols(artist);
	}

	/**
	 * get song title with special symbols
	 */
	public String getTitleForFile() {
		return replaceFileSymbols(title);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;

		if ((obj == null) || (obj.getClass() != this.getClass()))
			return false;

		Song song = (Song) obj;
		return title.equalsIgnoreCase(song.getTitle())
				&& artist.equalsIgnoreCase(song.getArtist());
	}

	/**
	 * replaces special symbols (for local archive)
	 */
	private String replaceFileSymbols(String in) {
		StringBuilder sb = new StringBuilder(in);
		StringBuilder out = new StringBuilder(in.length());
		// \ / ? : * " > < |
		for (int i = 0; i < sb.length(); i++) {
			char c = sb.charAt(i);
			switch (c) {
			case '\\':
				out.append("%0&");
				break;
			case '/':
				out.append("%1&");
				break;
			case '?':
				out.append("%2&");
				break;
			case ':':
				out.append("%3&");
				break;
			case '*':
				out.append("%4&");
				break;
			case '"':
				out.append("%5&");
				break;
			case '>':
				out.append("%6&");
				break;
			case '<':
				out.append("%7&");
				break;
			case '|':
				out.append("%8&");
				break;
			default:
				out.append(c);
			}
		}
		return out.toString();
	}

	/**
	 * replaces special symbols (to a link format)
	 */
	private String replaceSpecialSymbols(String in) {
		StringBuilder sb = new StringBuilder(in);
		StringBuilder out = new StringBuilder(in.length());
		for (int i = 0; i < sb.length(); i++) {
			char c = sb.charAt(i);
			switch (c) {
			case ' ':
				out.append('_');
				break;
			case '&':
				out.append("%26");
				break;
			case '?':
				out.append("%3F");
				break;
			case '\'':
				out.append("%27");
				break;
			default:
				out.append(c);
			}
		}
		return out.toString();
	}
}
